﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using LightManager.Effects;

namespace LightManager.UserControls
{
    public partial class ActionMakerControl : UserControl
    {
        public ActionMakerControl()
        {
            InitializeComponent();

            this.pctEffectArmor.Tag = new ArmorEffect();
            this.pctEffectCustom.Tag = new CustomStatusEffect();
            this.pctEffectDamage.Tag = new DamageEffect();
            this.pctEffectHeal.Tag = new HealingEffect();
            this.pctEffectShield.Tag = null;
        }

        public ICollection<Unit> Units
        {
            set
            {
                this.flpUnitList.Controls.Clear();
                foreach (Unit unit in value)
                {
                    PictureBox box = new PictureBox();
                    box.Margin = this.pctChar.Margin;
                    box.MouseDown += this.DraggableControl_MouseDown;
                    box.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
                    box.Size = this.pctChar.Size;
                    box.SizeMode = PictureBoxSizeMode.StretchImage;
                    if (unit.ImageFile != null)
                        box.Image = Image.FromFile(System.IO.Path.Combine(Application.StartupPath, unit.ImageFile));
                    box.Tag = unit;
                    this.flpUnitList.Controls.Add(box);
                }
            }
        }

        private IGameEffect ActiveEffect
        {
            get
            {
                if (this.flpEffectList.Controls.Count > 0)
                    return (IGameEffect)this.flpEffectList.Controls.OfType<PictureBox>().First().Tag;
                return null;
            }
        }

        private Unit ActiveTarget
        {
            get
            {
                if (this.flpUnitList.Controls.Count > 0)
                    return (Unit)this.flpUnitList.Controls.OfType<PictureBox>().First().Tag;
                return null;
            }
        }

        private void DraggableControl_MouseDown(object sender, MouseEventArgs e)
        {
            PictureBox item = (PictureBox)sender;
            item.DoDragDrop(item, DragDropEffects.Link);
        }

        private void GenericUnitDrop_DragEnter(object sender, DragEventArgs e)
        {
            PictureBox pb = e.Data.GetData(typeof(PictureBox)) as PictureBox;
            if (pb != null)
                if (pb.Tag is Unit)
                {
                    e.Effect = DragDropEffects.Link;
                    return;
                }
            e.Effect = DragDropEffects.None;
        }

        private void flpUnit_DragDrop(object sender, DragEventArgs e)
        {
            foreach (PictureBox c in flpUnit.Controls.OfType<PictureBox>())
            {
                c.Parent = this.flpUnitList;
                c.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            }
            PictureBox data = (PictureBox)e.Data.GetData(typeof(PictureBox));
            data.Parent = this.flpUnit;
            data.BorderStyle = System.Windows.Forms.BorderStyle.None;
        }

        private void flpUnitList_DragDrop(object sender, DragEventArgs e)
        {
            PictureBox data = (PictureBox)e.Data.GetData(typeof(PictureBox));
            data.Parent = this.flpUnitList;
            data.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        }

        private void GenericEffectDrop_DragEnter(object sender, DragEventArgs e)
        {
            PictureBox pb = e.Data.GetData(typeof(PictureBox)) as PictureBox;
            if (pb != null)
                if (pb.Tag is IGameEffect)
                {
                    e.Effect = DragDropEffects.Link;
                    return;
                }
            e.Effect = DragDropEffects.None;
        }

        private void flpEffect_DragDrop(object sender, DragEventArgs e)
        {
            foreach (PictureBox c in flpEffect.Controls.OfType<PictureBox>())
            {
                c.Parent = this.flpUnitList;
                c.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            }
            PictureBox data = (PictureBox)e.Data.GetData(typeof(PictureBox));
            data.Parent = this.flpEffect;
            data.BorderStyle = System.Windows.Forms.BorderStyle.None;
        }

        private void flpEffectList_DragDrop(object sender, DragEventArgs e)
        {
            PictureBox data = (PictureBox)e.Data.GetData(typeof(PictureBox));
            data.Parent = this.flpEffectList;
            data.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
        }

        private void btnExecute_Click(object sender, EventArgs e)
        {
            Action action = new Action(this.ActiveEffect);
            action.Target = this.ActiveTarget;
        }
    }
}
